package com.atguigu.controller;

import com.atguigu.common.result.R;
import com.atguigu.config.AuthContextHolder;
import com.atguigu.service.PatientService;
import com.atguigu.yygh.model.user.Patient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/api/user/patient")
public class PatientController {
    @Autowired
    private PatientService patientService;
    //1获取就诊人列表
    @GetMapping("/auth/findAll")
    public R findAll(HttpServletRequest  request){
        Long userId = AuthContextHolder.getUserId(request);
        List<Patient> list = patientService.findListByUserId(userId);
        return  R.ok().data("list",list) ;
    }
    //2添加就诊人
    @PostMapping("auth/save")
    public  R savePatient(@RequestBody Patient patient,HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        patientService.save(patient);
        return  R.ok();
    }
    //根据id来获取就诊人的信息
    @GetMapping("/auth/get/{id}")
    public R getPatient(@PathVariable Long id) {
        Patient patient = patientService.getPatientById(id);
        return  R.ok().data("patient",patient);
    }

    @GetMapping("/auth/getPatientVo/{id}")
    public Patient getPatientVo(@PathVariable Long id) {
        Patient patient = patientService.getPatientById(id);
        return  patient;
    }
    //删除就诊人
    @DeleteMapping("auth/remove/{id}")
    public R removeById(@PathVariable Long id){
        patientService.removeById(id);
        return  R.ok();
    }
    //修改就诊人
    @PostMapping("auth/update")
    public R updatePatient(@RequestBody Patient patient) {
        patientService.updateById(patient);
        return R.ok();
    }




}
